﻿Imports System.ComponentModel
Imports DevExpress.Skins
Imports DevExpress.LookAndFeel
Imports DevExpress.UserSkins
Imports DevExpress.XtraBars
Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraBars.Helpers
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.Data
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid

Public Class KH_HangHoa1
    Dim Ma As String
    Dim State As Integer = 0 'trạng thái form '0 mặc định'1 tạo mới'2 sửa '3 xoa
    Dim DoanhThu, PhanTramThue, TienThue, TongCong As Decimal
    Private Sub setReadonly(ByVal b As Boolean)
        Me.SochungtuTextEdit.Properties.ReadOnly = b
        Me.NgaychungtuDateEdit.Properties.ReadOnly = b
        Me.NgaychungtuDateEdit.Properties.ReadOnly = b
        Me.MaDoiTuongSearchLookUpEdit.Properties.ReadOnly = b
        Me.TenkhachhangTextEdit.Properties.ReadOnly = b
        Me.DiachiTextEdit.Properties.ReadOnly = b
        Me.TaiKhoanNganHangSearchLookUpEdit.Properties.ReadOnly = b
        Me.DieuKienGiaoHangTextEdit.Properties.ReadOnly = b
        Me.TenNguoiGiaoDichnTextEdit.Properties.ReadOnly = b
        Me.BoPhanSearchLookUpEdit.Properties.ReadOnly = b
        Me.MaNSXSearchLookUpEdit.Properties.ReadOnly = b
        Me.TinhtrangSearchLookUpEdit.Properties.ReadOnly = b
        Me.NgayThanhToanDateEdit.Properties.ReadOnly = b
        Me.SoThamChieuTextEdit.Properties.ReadOnly = b
        Me.NoiDungTextEdit.Properties.ReadOnly = b
        Me.LoaitienSearchLookUpEdit.Properties.ReadOnly = b
        Me.NguyenteTextEdit.Properties.ReadOnly = b
        Me.ThanhtienvndTextEdit.Properties.ReadOnly = b
        Me.TygianganhangTextEdit.Properties.ReadOnly = b
        If b Then
            Me.IGridViewTienChiTiet.OptionsBehavior.Editable = False
        Else
            Me.IGridViewTienChiTiet.OptionsBehavior.Editable = True
        End If
        If b Then 'trong trạng thái readonly  thì không cho nhập và dòng trống thêm ở cuối
            Me.IGridViewTienChiTiet.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
            Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        Else  'cho thêm dòng trống ở cuối (khác form đơn - bỏ dòng : And State = 1)
            Me.IGridViewTienChiTiet.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
            Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        End If
        If b Then
            'dxprovidervalidation
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Manual
            setButtonTrangThaiSua(False)
        Else
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Auto
            setButtonTrangThaiSua(True)
        End If
    End Sub
    Private Sub setButtonTrangThaiSua(ByVal b As Boolean) 'set nút lưu và xóa - khi ở trạng thái sửa thì mới cho phép thao tác trên nó (enable = true) - các trạng thái khác thì không cho (enable = false)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then 'xử lý nút sửa (nếu nút lưu - xóa cho phép thì sửa ko cho phép và ngược lại)
            Me.BarButtonItemSua.Enabled = False
        Else
            Me.BarButtonItemSua.Enabled = True
        End If
        setButtonChiTietTrangThaiSua(b)
    End Sub
    Private Sub setButtonChiTietTrangThaiSua(ByVal b As Boolean)
        Me.BarButtonItemTaoMoiChiTiet.Enabled = b
        Me.BarButtonItemXoaChiTiet.Enabled = b
        Me.BarButtonItemHuyThayDoiChiTiet.Enabled = b
    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.KHHangHoaTableAdapter.Fill(Me.PCV_FISHDataSet.KHHangHoa)
        Me.KHHangHoaChiTietTableAdapter.Fill(Me.PCV_FISHDataSet.KHHangHoaChiTiet)
        Me.DmkhachhangTableAdapter.Fill(Me.PCV_FISHDataSet.dmkhachhang)
        Me.DmnghiepvuTableAdapter.Fill(Me.PCV_FISHDataSet.dmnghiepvu)
        Me.GetPhanLoai_TinhTrangPhieuTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_TinhTrangPhieu)
        Me.DMTaiKhoanKeToanCap2TableAdapter.Fill(Me.PCV_FISHDataSet.DMTaiKhoanKeToanCap2)
        Me.DmnguonvonTableAdapter.Fill(Me.PCV_FISHDataSet.dmnguonvon)
        Me.DmkheuocvayTableAdapter.Fill(Me.PCV_FISHDataSet.dmkheuocvay)
        Me.DmkhoanmucphiTableAdapter.Fill(Me.PCV_FISHDataSet.dmkhoanmucphi)
        Me.DmnganhangTableAdapter.Fill(Me.PCV_FISHDataSet.dmnganhang)
        Me.DmhanghoaTableAdapter.Fill(Me.PCV_FISHDataSet.dmhanghoa)
        Me.KHThueTableAdapter.Fill(Me.PCV_FISHDataSet.KHThue)
        Me.DmkhoTableAdapter.Fill(Me.PCV_FISHDataSet.dmkho)
        setReadonly(True)
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub KH_HangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.loadDuLieu()
        Me.TC_TienIGridControl.ForceInitialize()
        Me.KHHangHoaBindingSource.MoveLast()
        'Me.IGridViewTienChiTiet.Columns("taikhoanno").GroupIndex = 1
        'Me.IGridViewTienChiTiet.Columns("sohoadon").GroupIndex = 2
        'Me.IGridViewTienChiTiet.GetGroupSummaryValues(3)
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        'focus
        Me.setReadonly(False)
        'Mã //set ngầm
        Dim row As DataRowView = CType(KHHangHoaBindingSource.AddNew(), DataRowView)
        Me.Ma = "TI" & System.Guid.NewGuid.ToString()
        row("ma") = Ma
        row("manghiepvu") = "MHTN"
        'mặc định
        Me.NgaychungtuDateEdit.EditValue = Today
        Me.NgayThanhToanDateEdit.EditValue = Today
        Me.TygianganhangTextEdit.EditValue = 1
        Me.LoaitienSearchLookUpEdit.EditValue = "VND"
        Me.ThanhtienvndTextEdit.EditValue = "0"
        Me.NguyenteTextEdit.EditValue = "0"
    End Sub

    Private Sub MakhachhangSearchLookUpEdit_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MaDoiTuongSearchLookUpEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.MaDoiTuongSearchLookUpEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TenkhachhangTextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ten")
        Me.DiachiTextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "diachi")
    End Sub

    Private Sub BarButtonItemLuu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridViewThue.PostEditor()
            Me.IGridViewThue.UpdateCurrentRow()
            Me.IGridViewThue.CloseEditor()
            Me.IGridViewTienChiTiet.PostEditor()
            Me.IGridViewTienChiTiet.UpdateCurrentRow()
            Me.IGridViewTienChiTiet.CloseEditor()
            Me.KHHangHoaBindingSource.EndEdit() 'end edit trước khi lưu - ghi những thay đổi 
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.EndEdit()
            Me.FKKHThueKHHangHoaBindingSource.EndEdit()
            'Me.FKTCThueTCThue1BindingSource.EndEdit()
            If Me.PCV_FISHDataSet.HasChanges Then 'coi có thay đổi dữ liệu hay ko ?
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Try
                        Me.TableAdapterManager.UpdateAll(Me.PCV_FISHDataSet)
                        Me.State = 0 'trở về trạng thái mặc định 0
                        Me.setReadonly(True) 'set lại readonly
                        setButtonTrangThaiSua(False) 'set lại nút 
                    Catch ex As Exception
                        DevExpress.XtraEditors.XtraMessageBox.Show("Xảy ra lỗi :" & ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Else 'nếu ko thay đổi dữ liệu
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.setReadonly(True) 'set lại readonly
                setButtonTrangThaiSua(False) 'set lại nút 
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message) 'tung message khi có ngoại lệ
        End Try
    End Sub

    Private Sub NhanviensaleSearchLookUpEdit_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles BoPhanSearchLookUpEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.BoPhanSearchLookUpEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.DmphongbanBindingSource.Filter = "ma = '" & editor.Properties.View.GetRowCellValue(rowhandle, "bophan") & "'"
    End Sub

    Private Sub BarButtonItemSua_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setReadonly(False)
        Me.State = 2
    End Sub

    Private Sub IGridView1_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewTienChiTiet.CellValueChanged
        If e.Column.FieldName = "dongianguyente" Or e.Column.FieldName = "soluonggiao" Or e.Column.FieldName = "dongiavnd" Or e.Column.FieldName = "tilethueGTGT" Then
            Try
                IGridViewTienChiTiet.SetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("thanhtiennguyente"), CDec(Me.IGridViewTienChiTiet.GetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("soluonggiao"))) * CDec(Me.IGridViewTienChiTiet.GetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("dongianguyente"))))
            Catch ex As Exception
                IGridViewTienChiTiet.SetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("thanhtiennguyente"), 0)
            End Try
            Try
                IGridViewTienChiTiet.SetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("thanhtienvnd"), CDec(Me.IGridViewTienChiTiet.GetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("soluonggiao"))) * CDec(Me.IGridViewTienChiTiet.GetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("dongiavnd"))))
            Catch ex As Exception
                IGridViewTienChiTiet.SetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("thanhtienvnd"), 0)
            End Try
            Try
                IGridViewTienChiTiet.SetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("tienthueGTGT"), CDec(Me.IGridViewTienChiTiet.GetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("thanhtienvnd"))) * CDec(Me.IGridViewTienChiTiet.GetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("tilethueGTGT"))) / 100)
            Catch ex As Exception
                IGridViewTienChiTiet.SetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("tienthueGTGT"), 0)
            End Try
        End If
        Me.IGridViewTienChiTiet.UpdateSummary()
    End Sub

    Private Sub IGridViewTienChiTiet_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles IGridViewTienChiTiet.FocusedRowChanged
        Dim currentView As GridView = CType(sender, GridView)
        ' The group row's handle. 
        Dim groupRowHandle As Integer
        If (e.FocusedRowHandle >= 0) Then
            groupRowHandle = currentView.GetParentRowHandle(e.FocusedRowHandle)
        Else
            groupRowHandle = e.FocusedRowHandle
        End If
        If Not currentView.IsGroupRow(groupRowHandle) Then Return

        ' Get group summary values. 
        Dim ht As Hashtable = currentView.GetGroupSummaryValues(groupRowHandle)
        Text = ""
        ' Iterate through group summaries. 
        Dim entry As DictionaryEntry
        For Each entry In ht
            Dim sumItem As GridGroupSummaryItem = entry.Key
            Dim val As Object = entry.Value
            ' Get the summry item's Tag property and summary value. 
            Try
                'Text += sumItem.Tag.ToString() + ": " + val.ToString() + "\t"
                MessageBox.Show(val.ToString)
            Catch ex As Exception

            End Try

        Next
    End Sub
    'Private Sub InitStartValue()
    '    Me.TienThue = 0
    'End Sub
    'Private Sub IGridViewTienChiTiet_CustomSummaryCalculate(ByVal sender As Object, ByVal e As DevExpress.Data.CustomSummaryEventArgs) Handles IGridViewTienChiTiet.CustomSummaryCalculate
    '    'tien thue
    '    'If e.SummaryProcess = CustomSummaryProcess.Start Then
    '    '    InitStartValue()
    '    'End If
    '    'If e.SummaryProcess = CustomSummaryProcess.Calculate Then
    '    '    For index = 0 To Me.IGridViewTienChiTiet.RowCount - 1
    '    '        If Me.IGridViewTienChiTiet.GetRowCellValue(index, "taikhoanno").ToString = "133" Then
    '    '            Me.TienThue += CDec(e.FieldValue)
    '    '        End If
    '    '    Next
    '    'End If
    'End Sub
    Private Sub IGridView1_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewTienChiTiet.InitNewRow
        Me.KHHangHoaBindingSource.EndEdit()
        Try
            Me.IGridViewTienChiTiet.SetFocusedRowCellValue("ma", Ma)
            Me.IGridViewTienChiTiet.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
            Me.IGridViewTienChiTiet.SetFocusedRowCellValue("manghiepvu", "")
            'Me.IGridViewTienChiTiet.SetFocusedRowCellValue("sochungtu", Me.SochungtuTextEdit.EditValue)
            'Me.IGridViewTienChiTiet.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuDateEdit.EditValue))
            'Me.IGridViewTienChiTiet.SetFocusedRowCellValue("phantramthue", 0) 'phân biệt chữ hoa thường
            'Me.IGridViewTienChiTiet.SetFocusedRowCellValue("nguyente", 0)
            'Me.IGridViewTienChiTiet.SetFocusedRowCellValue("tienvnd", 0)
            'Me.IGridViewTienChiTiet.SetFocusedRowCellValue("tienthue", 0)
            'Me.IGridViewTienChiTiet.SetFocusedRowCellValue("tongcong", 0)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Dispose() 'giải phóng ram
        Me.Close() 'đóng
    End Sub

    Private Sub BarButtonItemTaoMoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoiChiTiet.ItemClick
        Me.IGridViewTienChiTiet.MoveLast()
        Me.IGridViewTienChiTiet.Focus()
        Me.IGridViewTienChiTiet.AddNewRow()
        Me.IGridViewTienChiTiet.FocusedColumn = Me.IGridViewTienChiTiet.VisibleColumns(0)
        Me.IGridViewTienChiTiet.ShowEditor()
    End Sub

    Private Sub RepositoryItemSearchLookUpEdit5_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs)
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        IGridViewTienChiTiet.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewTienChiTiet.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "ten1"))
        IGridViewTienChiTiet.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowHandle, "donvitinh"))
    End Sub

    Private Sub MakhachhangSearchLookUpEdit_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaDoiTuongSearchLookUpEdit.Enter
        Try
            SendKeys.Send("%{DOWN}")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub NgaychungtuDateEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles NgaychungtuDateEdit.GotFocus
        Me.NgaychungtuDateEdit.ShowPopup()
    End Sub

    Private Sub NhanviensaleSearchLookUpEdit_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles BoPhanSearchLookUpEdit.Enter
        If IsDBNull(Me.BoPhanSearchLookUpEdit.EditValue) Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub PhongbanSearchLookUpEdit_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaNSXSearchLookUpEdit.Enter
        If IsDBNull(Me.MaNSXSearchLookUpEdit.EditValue) Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub HinhthucthanhtoanSearchLookUpEdit_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TaiKhoanNganHangSearchLookUpEdit.Enter
        If IsDBNull(Me.TaiKhoanNganHangSearchLookUpEdit.EditValue) Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub LoaitienSearchLookUpEdit_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoaitienSearchLookUpEdit.Enter
        If IsDBNull(Me.LoaitienSearchLookUpEdit.EditValue) Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub TinhtrangSearchLookUpEdit_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TinhtrangSearchLookUpEdit.Enter
        If IsDBNull(Me.TinhtrangSearchLookUpEdit.EditValue) Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub BarButtonItemFirst_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.KHHangHoaBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonPrevious_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonPrevious.ItemClick
        Me.KHHangHoaBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonNext_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonNext.ItemClick
        Me.KHHangHoaBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonLast_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonLast.ItemClick
        Me.KHHangHoaBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemFirstChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirstChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.MoveFirst()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemPreviousChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemPreviousChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.MovePrevious()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemNextChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNextChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.MoveNext()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemLastChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLastChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.MoveLast()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub
    '============= bắt lỗi igridview1 =========================
    Private Sub IGridView1_ShownEditor(ByVal sender As Object, ByVal e As System.EventArgs) Handles IGridViewTienChiTiet.ShownEditor
        Dim view As ColumnView = CType(sender, ColumnView)
        view.ActiveEditor.IsModified = True
        If TypeOf Me.IGridViewTienChiTiet.ActiveEditor Is SearchLookUpEdit And IsDBNull(Me.IGridViewTienChiTiet.FocusedValue) Then
            DirectCast(IGridViewTienChiTiet.ActiveEditor, SearchLookUpEdit).ShowPopup()
        End If
    End Sub

    Private Sub IGridView1_ValidatingEditor(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs) Handles IGridViewTienChiTiet.ValidatingEditor
        If Me.IGridViewTienChiTiet.FocusedColumn.FieldName = "taikhoanno" And IGridViewTienChiTiet.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewTienChiTiet.FocusedColumn.FieldName = "taikhoanco" And IGridViewTienChiTiet.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewTienChiTiet.FocusedColumn.FieldName = "mahang" And IGridViewTienChiTiet.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewTienChiTiet.FocusedColumn.FieldName = "solo" And IGridViewTienChiTiet.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewTienChiTiet.FocusedColumn.FieldName = "hsd" And IGridViewTienChiTiet.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewTienChiTiet.FocusedColumn.FieldName = "soluongSO" And IGridViewTienChiTiet.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf CDec(e.Value) = 0 Then
                e.ErrorText = "không được bằng 0 !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewTienChiTiet.FocusedColumn.FieldName = "soluonggiao" And IGridViewTienChiTiet.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf CDec(e.Value) = 0 Then
                e.ErrorText = "không được bằng 0 !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewTienChiTiet.FocusedColumn.FieldName = "tilethueGTGT" And IGridViewTienChiTiet.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewTienChiTiet.FocusedColumn.FieldName = "tienthueGTGT" And IGridViewTienChiTiet.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            End If
        End If
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa số chứng từ '" & Me.SochungtuTextEdit.EditValue & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.KHHangHoaBindingSource.RemoveCurrent()
            'Me.TCThueTableAdapter.Update(Me.PCV_FISHDataSet.TCThue)
            Me.setReadonly(True)
            Me.State = 0 'trở về mặc định
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.KHHangHoaBindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemXoaChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoaChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.IGridViewTienChiTiet.GetRowCellValue(Me.IGridViewTienChiTiet.FocusedRowHandle, "mahang") & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewTienChiTiet.DeleteRow(Me.IGridViewTienChiTiet.FocusedRowHandle)
            End If
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoiChiTiet.ItemClick
        Me.FKKHHangHoaChiTietKHHangHoaBindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi của phần 'hàng hóa' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        'load dữ liệu lại nhưng giữ nguyên phiếu
        Dim position As Integer = Me.KHHangHoaBindingSource.Position
        loadDuLieu()
        Me.KHHangHoaBindingSource.Position = position
    End Sub

    Private Sub RepositoryItemSearchLookUpEditKhachHang_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEditKhachHang.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("madonvi", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("tendonvi", editor.Properties.View.GetRowCellValue(rowHandle, "ten"))
    End Sub

    Private Sub RepositoryItemSearchLookUpEditKheUocVay_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEditKheUocVay.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("kheuocvay", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        Me.IGridViewTienChiTiet.SetFocusedRowCellValue("hopdongtindung", editor.Properties.View.GetRowCellValue(rowHandle, "sohopdongtindung"))
    End Sub

    Private Sub BarButtonItemChuyenDuLieuThue_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemChuyenDuLieuThue.ItemClick
        'group by 
        'For index = 0 To Me.IGridViewTienChiTiet.RowCount - 1
        '    MessageBox.Show(Me.IGridViewTienChiTiet.GetGroupRowValue(index).ToString)
        'Next
        If Me.IGridViewTienChiTiet.GetGroupRowValue(Me.IGridViewTienChiTiet.FocusedRowHandle) Then

        End If
        Try
            For index = 0 To Me.IGridViewTienChiTiet.RowCount - 1
                If Me.IGridViewTienChiTiet.GetRowCellValue(index, "taikhoanno").ToString = "133" Then
                    TienThue += CDec(Me.IGridViewTienChiTiet.GetRowCellValue(index, "tienvnd"))
                End If
            Next
        Catch ex As Exception

        End Try
        Me.TongCong = CDec(Me.IGridViewTienChiTiet.Columns("tienvnd").SummaryItem.SummaryValue)
        Try
            Me.PhanTramThue = TienThue / TongCong * 100
        Catch ex As Exception
            Me.PhanTramThue = 0
            MessageBox.Show(ex.Message) 'loại trừ chia cho 0
        End Try
        Me.DoanhThu = TongCong - TienThue
        Me.IGridViewThue.AddNewRow()
        'bỏ group by
    End Sub

    Private Sub IGridViewThue_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewThue.InitNewRow
        'set mặc định
        Me.IGridViewThue.SetFocusedRowCellValue("ma", Ma)
        Me.IGridViewThue.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewThue.SetFocusedRowCellValue("manghiepvu", "MHTN")
        Me.IGridViewThue.SetFocusedRowCellValue("sochungtu", Me.SochungtuTextEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuDateEdit.EditValue))
        '==================================================================
        Me.IGridViewThue.SetFocusedRowCellValue("doanhthu", Me.DoanhThu)
        Me.IGridViewThue.SetFocusedRowCellValue("phantramthue", Me.PhanTramThue) 'phân biệt chữ hoa thường
        Me.IGridViewThue.SetFocusedRowCellValue("tienthue", Me.TienThue)
        Me.IGridViewThue.SetFocusedRowCellValue("tongcong", Me.TongCong)
    End Sub

End Class
